package org.krivtsun.scanlab.editor;

import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;

/**
 * next Add histogram panels
 *  
 * @author Andrew Krivtsun
 */
public class ImageEditor extends JScrollPane {

    private Rule columnView = new Rule(Rule.HORIZONTAL);
    private Rule rowView = new Rule(Rule.VERTICAL);
    private ImageView view = new ImageView();

    private ScaleManager scaleManager = new ScaleManager(getViewport());
    private Action rulesAction;

    public ImageEditor() {
        super(VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_ALWAYS);
        setBorder(BorderFactory.createTitledBorder("Image"));

//        //Create the corners.
//        JPanel buttonCorner = new JPanel(); //use FlowLayout
//        JToggleButton isMetric = new JToggleButton("Ru", true);
//        isMetric.setFont(new Font("SansSerif", Font.PLAIN, 11));
//        isMetric.setMargin(new Insets(2,2,2,2));
//        //isMetric.addItemListener(this);
//        buttonCorner.add(isMetric);

        //setCorner(JScrollPane.UPPER_LEFT_CORNER, buttonCorner);
        //setCorner(JScrollPane.LOWER_LEFT_CORNER, new Corner());
        //setCorner(JScrollPane.UPPER_RIGHT_CORNER, new Corner());

//        columnView.setPreferredSize(columnView.getPreferredSize());
//        rowView.setPreferredSize(rowView.getPreferredSize());

        rulesAction = createRulersAction();

        setViewportView(view);

        scaleManager.setScaleChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                rowView.setScale(scaleManager.getScale());
                columnView.setScale(scaleManager.getScale());
                view.setScale(scaleManager.getScale());
            }
        });
    }

    public void setImage(RenderedImage img) {
        rowView.setImage(img);
        columnView.setImage(img);
        scaleManager.setImage(img);
        view.setImage(img);

        boolean hasData = img != null;
        rulesAction.setEnabled(hasData);
    }

    public Component getViewComponent() {
        return this;
    }

    public JComponent getViewStatusBarItem() {
        return view.getStatusBarItem();
    }

    public JComponent getScaleStatusBarItem() {
        return scaleManager.getStatusBarItem();
    }

    public JMenu getMenu() {
        JMenu menu = new JMenu("Image");
        menu.setMnemonic('i');
        menu.add(scaleManager.getMenu());
        menu.add(new JCheckBoxMenuItem(rulesAction));
        return menu;
    }

    private Action createRulersAction() {
        Action action = new AbstractAction("Rulers") {
            public void actionPerformed(ActionEvent e) {
                JCheckBoxMenuItem item = (JCheckBoxMenuItem)e.getSource();
                showRules(item.isSelected());
            }
        };
        action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl R"));
        action.putValue(Action.MNEMONIC_KEY, new Integer('R'));
        action.setEnabled(false);
        return action;
    }

    private void showRules(boolean show) {
        setColumnHeaderView(show? columnView: null);
        setRowHeaderView(show? rowView: null);
    }
}
